{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "blob: 95 : 47.0 28.14\n",
      "blob: 94 : 112.5 48.87\n",
      "blob: 93 : 163.0 60.97\n",
      "blob: 92 : 2.0 6.83\n",
      "blob: 91 : 11.0 15.66\n",
      "blob: 90 : 103.0 48.63\n",
      "blob: 89 : 165.5 62.53\n",
      "blob: 88 : 35.0 25.31\n",
      "blob: 87 : 76.0 39.31\n",
      "blob: 86 : 99.0 46.63\n",
      "blob: 85 : 176.0 65.11\n",
      "blob: 84 : 123.0 53.11\n",
      "blob: 83 : 157.0 61.80\n",
      "blob: 82 : 169.5 60.53\n",
      "blob: 81 : 187.0 64.77\n",
      "blob: 80 : 167.0 62.63\n",
      "blob: 79 : 375.0 122.71\n",
      "blob: 78 : 117.5 53.70\n",
      "blob: 77 : 168.5 58.18\n",
      "blob: 76 : 141.5 54.18\n",
      "blob: 75 : 161.0 62.63\n",
      "blob: 74 : 202.0 68.28\n",
      "blob: 73 : 185.0 65.25\n",
      "blob: 72 : 178.5 67.36\n",
      "blob: 71 : 180.0 64.77\n",
      "blob: 70 : 133.0 51.60\n",
      "blob: 69 : 162.0 61.11\n",
      "blob: 68 : 139.5 52.18\n",
      "blob: 67 : 170.5 67.70\n",
      "blob: 66 : 200.0 70.08\n",
      "blob: 65 : 162.5 62.53\n",
      "blob: 64 : 106.0 45.46\n",
      "blob: 63 : 167.0 60.43\n",
      "blob: 62 : 180.0 65.80\n",
      "blob: 61 : 192.5 68.38\n",
      "blob: 60 : 181.0 67.25\n",
      "blob: 59 : 174.5 62.87\n",
      "blob: 58 : 359.5 131.64\n",
      "blob: 57 : 89.0 40.97\n",
      "blob: 56 : 47.0 47.31\n",
      "blob: 55 : 192.5 66.04\n",
      "blob: 54 : 190.5 67.50\n",
      "blob: 53 : 213.0 70.91\n",
      "blob: 52 : 215.5 71.01\n",
      "blob: 51 : 192.0 64.97\n",
      "blob: 50 : 158.5 64.18\n",
      "blob: 49 : 333.5 121.84\n",
      "blob: 48 : 146.0 63.11\n",
      "blob: 47 : 168.5 64.87\n",
      "blob: 46 : 156.5 65.21\n",
      "blob: 45 : 181.0 64.63\n",
      "blob: 44 : 201.0 68.77\n",
      "blob: 43 : 161.0 64.63\n",
      "blob: 42 : 170.5 57.84\n",
      "blob: 41 : 175.5 64.04\n",
      "blob: 40 : 194.0 70.43\n",
      "blob: 39 : 146.0 58.63\n",
      "blob: 38 : 199.0 68.97\n",
      "blob: 37 : 155.5 56.38\n",
      "blob: 36 : 190.5 68.87\n",
      "blob: 35 : 133.5 56.87\n",
      "blob: 34 : 157.5 60.38\n",
      "blob: 33 : 192.0 70.43\n",
      "blob: 32 : 175.0 67.74\n",
      "blob: 31 : 191.0 66.97\n",
      "blob: 30 : 128.0 57.60\n",
      "blob: 29 : 164.0 62.97\n",
      "blob: 28 : 94.5 44.04\n",
      "blob: 27 : 242.0 101.40\n",
      "blob: 26 : 170.0 64.91\n",
      "blob: 25 : 181.0 66.28\n",
      "blob: 24 : 154.0 61.11\n",
      "blob: 23 : 180.0 62.43\n",
      "blob: 22 : 172.5 65.36\n",
      "blob: 21 : 61.0 32.49\n",
      "blob: 20 : 160.5 61.21\n",
      "blob: 19 : 193.5 65.50\n",
      "blob: 18 : 160.0 64.28\n",
      "blob: 17 : 133.5 52.53\n",
      "blob: 16 : 134.5 65.36\n",
      "blob: 15 : 134.0 56.63\n",
      "blob: 14 : 176.0 60.43\n",
      "blob: 13 : 156.0 60.77\n",
      "blob: 12 : 154.0 56.97\n",
      "blob: 11 : 72.0 41.80\n",
      "blob: 10 : 130.5 63.84\n",
      "blob: 9 : 2.0 5.66\n",
      "blob: 8 : 42.0 34.14\n",
      "blob: 7 : 45.5 34.73\n",
      "blob: 6 : 41.0 37.80\n",
      "blob: 5 : 77.5 48.53\n",
      "blob: 4 : 14.5 17.56\n",
      "blob: 3 : 84.0 47.60\n",
      "blob: 2 : 89.5 44.73\n",
      "blob: 1 : 31.0 31.80\n",
      "米粒数量： 95\n",
      "米粒面积均值： 148.78\n",
      "米粒长度均值： 58.72\n",
      "米粒面积方差： 4259.45\n",
      "米粒长度方差： 362.88\n",
      "米粒面积在3sigma范围内米粒的数量： 85\n",
      "米粒长度在3sigma范围内米粒的数量： 33\n"
     ]
    }
   ],
   "source": [
    "# %load E:\\Users\\ASUS\\PycharmProjects\\CV2\\ricecount.py\n",
    "import cv2 as cv\n",
    "import copy\n",
    "import math\n",
    "\n",
    "filename = r'E:\\data\\rice.png'\n",
    "img = cv.imread(filename)\n",
    "imgGray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)\n",
    "\n",
    "# 大津算法分割图像\n",
    "imgGray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)\n",
    "_ ,dst=cv.threshold(imgGray,0,0xff,cv.THRESH_OTSU)\n",
    "#dst= cv.adaptiveThreshold(imgGray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,5,2)\n",
    "#生成形态学操作中用到的核,返回指定形状和尺寸的结构元素\n",
    "element=cv.getStructuringElement(cv.MORPH_CROSS,(3,3))\n",
    "#形态学滤波去噪\n",
    "dst=cv.morphologyEx(dst,cv.MORPH_OPEN,element)\n",
    "\n",
    "seg=copy.deepcopy(dst)\n",
    "bin,cnts,hier=cv.findContours(seg,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)\n",
    "cv.drawContours(seg,cnts,-1,(120,0,0),2)  #绘制轮廓\n",
    "count=0 #米粒总数\n",
    "count1=0 #米粒面积在3sigma范围内米粒的数量\n",
    "count2=0 #米粒长度在3sigma范围内米粒的数量\n",
    "area_sum=0  #米粒总面积\n",
    "ricelength_sum =0 #米粒总长度\n",
    "area_Dsum=0  #米粒面积方差\n",
    "ricelength_Dsum=0 #米粒长度方差\n",
    "#遍历找到的所有米粒\n",
    "for i in range(len(cnts),0,-1):\n",
    "    c=cnts[i-1]\n",
    "    area = cv.contourArea(c) #计算包围性状的面积\n",
    "    ricelength=cv.arcLength(c,True) #计算包围性状的周长\n",
    "    #if area<10:   #过滤面积小于10的形状\n",
    "        #continue\n",
    "    count+=1    #总体计数加1\n",
    "    area_sum+=area\n",
    "    ricelength_sum+=ricelength\n",
    "    print(\"blob:\",i,\":\",area,\"{:.2f}\".format(ricelength)) #打印出每个米粒的面积和长度\n",
    "    x,y,w,h = cv.boundingRect(c) #提取矩形坐标\n",
    "    cv.rectangle(img,(x,y),(x+w,y+h),(0,0,0xff),1)#绘制矩形\n",
    "    cv.putText(img,str(count),(x,y),cv.FONT_HERSHEY_PLAIN,0.5,(0,0xff,0))\n",
    "#计算米粒面积和长度的方差\n",
    "for j in range(len(cnts),0,-1):\n",
    "    c=cnts[j-1]\n",
    "    area_D = (cv.contourArea(c)-area_sum/count)**2#计算包围性状的面积\n",
    "    ricelength_D=(cv.arcLength(c,True)-ricelength_sum/count)**2#计算包围性状的周长\n",
    "    area_Dsum+=area_D\n",
    "    ricelength_Dsum+=ricelength_D\n",
    "#计算3sigma范围内米粒的数量\n",
    "area_sigma=3*math.sqrt(area_Dsum/count)\n",
    "ricelength_sigma=3*math.sqrt(ricelength_Dsum/count)\n",
    "for k in range(len(cnts), 0, -1):\n",
    "    c = cnts[k- 1]\n",
    "    if  cv.contourArea(c)<area_sigma:\n",
    "        count1+=1\n",
    "    if  cv.arcLength(c,True)<ricelength_sigma:\n",
    "        count2+=1\n",
    "print(\"米粒数量：\",count)\n",
    "print(\"米粒面积均值：\",\"{:.2f}\".format(area_sum/count))\n",
    "print(\"米粒长度均值：\",\"{:.2f}\".format(ricelength_sum/count))\n",
    "print(\"米粒面积方差：\",\"{:.2f}\".format(area_Dsum/count))\n",
    "print(\"米粒长度方差：\",\"{:.2f}\".format(ricelength_Dsum/count))\n",
    "print(\"米粒面积在3sigma范围内米粒的数量：\",count1)\n",
    "print(\"米粒长度在3sigma范围内米粒的数量：\",count2)\n",
    "cv.imshow(\"源图\",img)\n",
    "cv.imshow(\"阈值化图\",dst)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
